package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.atguigu.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SeckillSessionDao;
import com.atguigu.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguigu.gulimall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {
        // 查询开始时间包含在这三天内的时间里
        List<SeckillSessionEntity> seckillSessionEntityList = this.list(new QueryWrapper<SeckillSessionEntity>()
                .between("start_time", startTime(), endTime()));

        if (seckillSessionEntityList != null && seckillSessionEntityList.size() > 0) {
            // 查询秒杀活动关联的秒杀商品信息
            List<SeckillSessionEntity> sessionEntityList = seckillSessionEntityList.stream().map(item -> {
                // 秒杀活动场次关联的秒杀商品信息
                List<SeckillSkuRelationEntity> skuRelationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>()
                        .eq("promotion_session_id", item.getId()));
                item.setRelationSkus(skuRelationEntities);
                return item;
            }).collect(Collectors.toList());
            return sessionEntityList;
        }
        return null;
    }


    /**
     * 获取最近三天内的最小时间
     * @return
     */
    public String startTime() {
        // 获取到当前日期 YYYY-MM-HH
        LocalDate now = LocalDate.now();
        // 获取到当前最小时间 0.00
        LocalTime min = LocalTime.MIN;
        // 从日期和时间中获取LocalDateTime的示例 YYYY-MM-HH:0.00 进行组装
        LocalDateTime start = LocalDateTime.of(now, min);
        // 组装后 2021-01-08T23:59:59.999999999 不符合时间规范，因此通过指定格式
        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    /**
     * 获取两天后最大时间
     * 23.59.59
     * @return
     */
    public String endTime(){
        // 获取到当前日期 YYYY-MM-HH
        LocalDate now = LocalDate.now();
        // 加上两天时间
        LocalDate localDate = now.plusDays(2);
        // 从日期和时间中获取LocalDateTime的示例 YYYY-MM-HH:0.00 进行组装
        LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MAX);
        // 组装后 2021-01-08T23:59:59.999999999 不符合时间规范，因此通过指定格式
        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }
}